﻿using System;
using System.Text;
using System.Data;
using System.IO;
using Portal.Xml.Utility;


namespace Portal.Cms.Model
{
    public class WeeklyScheduleCls
    {
        public string WeeklyScheduleId;
        public string WeekNumber;
        public string Year;
        public string Description;
        public string BelongSite;
        public string WeeklyScheduleItemText = "";
        public WeeklyScheduleItemCls[] WeeklyScheduleItems=new WeeklyScheduleItemCls[0];
    }

    public class WeeklyScheduleParser
    {
        public static WeeklyScheduleCls CreateInstance()
        {
            //tao 1 class trang....
            WeeklyScheduleCls oWeeklyScheduleCls = new WeeklyScheduleCls();
            return oWeeklyScheduleCls;
        }

        public static WeeklyScheduleCls ParseFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            WeeklyScheduleCls oWeeklyScheduleCls = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oWeeklyScheduleCls;
        }


        public static WeeklyScheduleCls ParseFromDataRow(DataRow dr)
        {
            WeeklyScheduleCls oWeeklyScheduleCls = new WeeklyScheduleCls();
            oWeeklyScheduleCls.WeeklyScheduleId = XmlUtility.GetString(dr, "WeeklyScheduleId", true);
            oWeeklyScheduleCls.WeekNumber = XmlUtility.GetString(dr, "WeekNumber", true);
            oWeeklyScheduleCls.Year = XmlUtility.GetString(dr, "Year", true);
            oWeeklyScheduleCls.Description = XmlUtility.GetString(dr, "Description", true);
            oWeeklyScheduleCls.BelongSite = XmlUtility.GetString(dr, "BelongSite", true);
            
            string WeeklyScheduleItemText = XmlUtility.GetString(dr, "XmlData", true);
            oWeeklyScheduleCls.WeeklyScheduleItemText = WeeklyScheduleItemText;
            oWeeklyScheduleCls.WeeklyScheduleItems = WeeklyScheduleItemParser.ParseMultiRecordFromXml(WeeklyScheduleItemText);
            return oWeeklyScheduleCls;
        }

        public static WeeklyScheduleCls[] ParseFromDataTable(DataTable dtTable)
        {
            WeeklyScheduleCls[] WeeklySchedules = new WeeklyScheduleCls[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                WeeklySchedules[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return WeeklySchedules;
        }

        public static DataSet CreateDataset()
        {
            DataSet ds = new DataSet();
            ds.Tables.Add("Info");
     
            ds.Tables["Info"].Columns.Add("WeeklyScheduleId");
            ds.Tables["Info"].Columns.Add("WeekNumber");
            ds.Tables["Info"].Columns.Add("Year");
            ds.Tables["Info"].Columns.Add("Description");
            ds.Tables["Info"].Columns.Add("BelongSite");
            ds.Tables["Info"].Columns.Add("WeeklyScheduleItemText");

            return ds;
        }

        public static string GetXml(WeeklyScheduleCls oWeeklyScheduleCls)
        {
            DataSet ds = CreateDataset();
            ds.Tables["Info"].Rows.Add(new object[]{
                oWeeklyScheduleCls.WeeklyScheduleId,
                oWeeklyScheduleCls.WeekNumber,
                oWeeklyScheduleCls.Year,
                oWeeklyScheduleCls.Description,
                oWeeklyScheduleCls.BelongSite,
                oWeeklyScheduleCls.WeeklyScheduleItemText
            });
            return ds.GetXml();
        }

        public static string GetXml(DataRow dr)
        {
            WeeklyScheduleCls oWeeklySchedule = WeeklyScheduleParser.ParseFromDataRow(dr);
            string Xml=WeeklyScheduleParser.GetXml(oWeeklySchedule);
            return Xml;
        }

        public static string GetXml(DataTable dt)
        {
            if (dt == null) return null;
            WeeklyScheduleCls[] WeeklySchedules = new WeeklyScheduleCls[dt.Rows.Count];
            for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
            {
                WeeklySchedules[iIndex] = WeeklyScheduleParser.ParseFromDataRow(dt.Rows[iIndex]);
            }
            string Xml = WeeklyScheduleParser.GetXml(WeeklySchedules);
            return Xml;
        }


        public static string GetXml(WeeklyScheduleCls[] WeeklySchedules)
        {
            if (WeeklySchedules == null)
            {
                //neu null thi khoi tao = mang rong de cho do loi...!
                WeeklySchedules = new WeeklyScheduleCls[0];
            }
            DataSet ds = CreateDataset();
            for (int iIndex = 0; iIndex < WeeklySchedules.Length; iIndex++)
            {
                WeeklyScheduleCls oWeeklyScheduleCls = WeeklySchedules[iIndex];
                ds.Tables["Info"].Rows.Add(new object[]
                {
                    oWeeklyScheduleCls.WeeklyScheduleId,
                    oWeeklyScheduleCls.WeekNumber,
                    oWeeklyScheduleCls.Year,
                    oWeeklyScheduleCls.Description,
                    oWeeklyScheduleCls.BelongSite,
                    oWeeklyScheduleCls.WeeklyScheduleItemText,
                });
            }
            string XmlData = ds.GetXml();
            return XmlData;
        }

        public static WeeklyScheduleCls[] ParseMultiRecordFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if(ds.Tables.Count==0)
            {
                return new WeeklyScheduleCls[0];
            }
            WeeklyScheduleCls[] WeeklySchedules = new WeeklyScheduleCls[ds.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
            {
                WeeklySchedules[iIndex] = ParseFromDataRow(ds.Tables[0].Rows[iIndex]);
            }
            return WeeklySchedules;
        }
    }
}
